package de.szut.tree;

import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * Streamable iterator for lazy evaluation
 * @author Marc Huisinga
 *
 * @param <T> - Type to generate
 */
public interface Generator<T> extends Iterable<T> {
	
	
	/**
	 * Offers a linear stream for this generator.
	 * @return stream
	 */
	public default Stream<T> stream() {
		return StreamSupport.stream(spliterator(), false);
	}
	
	/**
	 * Offers a parallel stream for this generator.
	 * @return stream
	 */
	public default Stream<T> parallelStream() {
		return StreamSupport.stream(spliterator(), true);
	}
	
	/**
	 * Builds an empty generator that will not throw an error when attempted to traverse.
	 * @return empty generator
	 */
	public static <T> Generator<T> empty() {
		return () -> {
			return new Iterator<T>() {

				@Override
				public boolean hasNext() {
					return false;
				}

				@Override
				public T next() {
					return null;
				}
				
			};
		};
	}
	
}
